/**
 * 
 */
package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.extension.Extension;
import org.alipay4j.model.extension.ExtensionArea;
import org.alipay4j.model.menu.LabelRule;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author heliang
 * 扩展区操作组件
 */
public class ExtensionComponent extends AbstractComponent {

	public ExtensionComponent(Alipay alipay) {
		super(alipay);
	}
	
	/**
	* @Title: 个性化扩展区创建接口 
	* @Description: 开发者可使用该接口创建一套个性化扩展区，符合该扩展区规则的用户进入生活号主页后会看到该个性化扩展区
	* @author heliang
	* @param areas 扩展区列表，最大条数为3
	* @param rules 标签规则，目前限定只能传入1条，在扩展区上线后，满足该标签规则的用户进入生活号首页，将看到该套扩展区。
	* @return extension_key 扩展区套id，创建个性化扩展区成功后，支付宝会将该字段返回，后续扩展区上下线或者扩展区删除都会用到这个值
	* @throws AlipayException 
	 */
	public String create(List<ExtensionArea> areas,List<LabelRule> rules) throws AlipayException {
	    if (ObjectHelper.isEmpty(areas)) {
            throw new IllegalArgumentException("areas can't be null or empty");
        }
	    if (ObjectHelper.isEmpty(rules)) {
            throw new IllegalArgumentException("rules can't be null or empty");
        }
        Map<String, String> body = new HashMap<String, String>();
        body.put("areas", JSONObject.toJSONString(areas));
        body.put("label_rule", JSONObject.toJSONString(rules));
        body.put("method", "    alipay.open.public.personalized.extension.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_personalized_extension_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_personalized_extension_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("extension_key");
    }
	
	/**
	 * 扩展区删除接口
	 * 开发者可调用该接口删除创建的个性化扩展区
	 * @param extension_key  一套扩展区的key，删除默认扩展区时传入default ，查询扩展区列表可以获得每套扩展区的key
	 * @return boolean
	 * @throws AlipayException
	 */
	public boolean delete(String extension_key) throws AlipayException {
		Map<String, String> body = new HashMap<String, String>();
		body.put("extension_key", extension_key);
		body.put("method", "alipay.open.public.personalized.extension.delete");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_personalized_extension_delete_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_personalized_extension_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return true;
	}
	
	/**
	* @Title: 扩展区批量查询接口 
	* @Description: 开发者可使用此接口批量查询个性化扩展区
	* @author heliang
	* @return List<Extension> 
	* @throws AlipayException 
	 */
	public List<Extension> batchquery() throws AlipayException {
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.personalized.extension.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_personalized_extension_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_personalized_extension_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        JSONArray array = bodyRes.getJSONArray("extensions");
        List<Extension> extensions = new ArrayList<Extension>();
        for(int i=0;i<array.size();i++) {
            Extension extension = JSONObject.toJavaObject(array.getJSONArray(i), Extension.class);
            extensions.add(extension);
        }
        return extensions;
    }

	/**
	* @Title: 默认扩展区创建接口 
	* @Description: 开发者可使用此接口创建默认扩展区，用户进入该生活号后，若未匹配到个性化扩展区，则展示默认扩展区
	* @author heliang
	* @param areas  默认扩展区列表，最多包含3个扩展区  
	* @return extension_key  一套扩展区的key，创建一套扩展区成功后，支付宝会将该字段返回，后续对扩展区进行删除等操作都会用到这个值。 
	* @throws AlipayException 
	 */
	public String defaultCreate(List<ExtensionArea> areas) throws AlipayException {
        if (ObjectHelper.isEmpty(areas)) {
            throw new IllegalArgumentException("areas can't be null or empty");
        }
        Map<String, String> body = new HashMap<String, String>();
        body.put("areas", JSONObject.toJSONString(areas));
        body.put("method", "alipay.open.public.default.extension.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_default_extension_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_default_extension_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("extension_key");
    }
}
